package zhengdaoyang.settings.web.controller;

import zhengdaoyang.settings.domain.Goods;
import zhengdaoyang.settings.service.GoodService;
import zhengdaoyang.settings.service.impl.GoodServiceImpl;
import zhengdaoyang.utils.PrintJson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet({"/settings/goods/listdata", "/settings/goods/findAllGoods",
             "/settings/goods/insertcart"})
public class goodsController extends HttpServlet {
    private final GoodService goodService = new GoodServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入到商品控制器");

        String path = request.getServletPath();

        System.out.println(path + "  1111111111");
        if ("/settings/goods/listdata".equals(path)) {
            listdata(request, response);
        } else if ("/settings/goods/findAllGoods".equals(path)) {
            findallgoods(request, response);
        } else if ("/settings/goods/insertcart".equals(path)) {
            insertcart(request, response);
        }
    }

    //向购物车添加商品
    private void insertcart(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始获取");
        String name = request.getParameter("goodsname");
        String p= request.getParameter("goodsprice");
        double price = Double.parseDouble(p);

        System.out.println(name+price+"=================");
        try{
            int i = goodService.insertcart(name,price);
            System.out.println("成功返回商品控制层");
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("成功加入购物车");
        PrintJson.printJsonFlag(response,true);

    }

    /*查找 分类 全部商品
     * */
    private void findallgoods(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始获取");
        String name = request.getParameter("category");
        //开始处理服务
        try {
            List<Goods> goods = goodService.findallgoods(name);
            System.out.println("成功返回商品控制层");
            /*讲对象解析为json 返回*/
            PrintJson.printJsonObj(response, goods);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /*查找商品
    param：name
    按照商品的名字获取商品信息*/
    private void listdata(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始获取");
        String name = request.getParameter("goodsname");
        //开始处理服务
        try {
            List<Goods> goods = goodService.getGoodDate(name);
            System.out.println("成功返回商品控制层");
            /*讲对象解析为json 返回*/
            PrintJson.printJsonObj(response, goods);

        } catch (Exception e) {
            e.printStackTrace();
        }


    }


}
